import Vue from 'vue'
import {cloneDeep} from 'lodash'

export const DASH_INDEX = 'DASH_INDEX'
export const OSS_CONFIG = 'OSS_CONFIG'
export const SEARCH_INDEX = 'SEARCH_INDEX'
export const SET_ABNORMAL = 'SET_ABNORMAL'

const state = {
  abnormal: false
}
const mutations = {
  [SET_ABNORMAL] (state) {
    state.abnormal = true
  }
}
const actions = {
  [DASH_INDEX] ({commit, dispatch}, data) {
    let _data = cloneDeep(data)
    return new Promise((resolve, reject) => {
      Vue.req.post('/http/saas/index.json', _data, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  [OSS_CONFIG] ({commit, dispatch}, data) {
    let _data = cloneDeep(data)
    return new Promise((resolve, reject) => {
      Vue.req.post('/http/common/ossConfig.json', _data, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }).then(res => {
        if (res.success) {
          Vue.localStorage.set(Vue.const.COOKIE.KEY_OSS, res.data, {
            maxAge: Vue.const.COOKIE.MAX_AGE
          })
          resolve(res)
        } else {
          resolve(res)
        }
      }).catch(err => {
        reject(err)
      })
    })
  },
  [SEARCH_INDEX] ({commit, dispatch}, data) {
    let _data = cloneDeep(data)
    return new Promise((resolve, reject) => {
      Vue.req.post('/http/saas/indexLog/search.json', _data, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  state,
  mutations,
  actions
}
